package com.hd.service.sqlexcel.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hd.dao.BaseDao;
import com.hd.entity.sqlexcel.SqlExcelReport;
import com.hd.service.sqlexcel.SqlExcelReportService;
import com.hd.util.StringUtils;
import com.hd.util.result.PageInfo;
/**
* 
*/
@Service("sqlExcelReportService")
public class SqlExcelReportServiceImpl implements SqlExcelReportService {
    private static final Integer STATUS=1;
    @Autowired
    private BaseDao<SqlExcelReport,String>sqlExcelReportDao;
    @Override
    public void save(SqlExcelReport sqlExcelReport) {
       sqlExcelReport.setId(UUID.randomUUID().toString());
       sqlExcelReport.setStatus(STATUS);
       sqlExcelReportDao.save(sqlExcelReport);
    }
    @Override
    public void delete(String id){
      sqlExcelReportDao.delete(SqlExcelReport.class,id);
    }
       

    @Override
    public void update(SqlExcelReport sqlExcelReport) {
        sqlExcelReport.setStatus(STATUS);
        sqlExcelReportDao.update(sqlExcelReport);
    }
    @Override
    public SqlExcelReport get(String id){
    	return sqlExcelReportDao.get(SqlExcelReport.class, id);
    }
        

    @Override
    public void selectDataGrid(PageInfo pageInfo) {
        String hql="from SqlExcelReport where 1=1 ";
        Map<String,Object>condition=pageInfo.getCondition();
        List<Object> params=new ArrayList<Object>();
        if(StringUtils.isNotNull(condition.get("name"))) {
        	hql+=" and name like ?";
        	params.add("%"+condition.get("name")+"%");
        }
        if(StringUtils.isNotNull(condition.get("type"))) {
        	hql+=" and type=?";
        	params.add(condition.get("type"));
        }
        hql+=" and status="+STATUS;
        if(!pageInfo.getSort().equals("")) {
            hql+=" order by "+pageInfo.getSort()+" "+pageInfo.getOrder();
        }
        List<SqlExcelReport> sqlExcelReports=sqlExcelReportDao.findPage(hql, pageInfo.getNowpage(), pageInfo.getPagesize(),params);
        pageInfo.setRows(sqlExcelReports);
        String countHql="select count(*) "+hql;
        int total=Integer.parseInt(sqlExcelReportDao.getCountByList(countHql,params).toString());
        pageInfo.setTotal(total);
    }

    @Override
    public void updateStatus(Integer status, String id) {
        String hql="update SqlExcelReport set status=? where id=?";
        sqlExcelReportDao.execute(hql,status,id);
    }
}
